<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Source code</title>
<link rel="stylesheet" type="text/css" href="../../../../../../stylesheet.css" title="Style">
</head>
<body>
<main role="main">
<div class="sourceContainer">
<pre><span class="sourceLineNo">001</span><a id="line.1">/*</a>
<span class="sourceLineNo">002</span><a id="line.2"> * Licensed to the Apache Software Foundation (ASF) under one or more</a>
<span class="sourceLineNo">003</span><a id="line.3"> * contributor license agreements.  See the NOTICE file distributed with</a>
<span class="sourceLineNo">004</span><a id="line.4"> * this work for additional information regarding copyright ownership.</a>
<span class="sourceLineNo">005</span><a id="line.5"> * The ASF licenses this file to You under the Apache License, Version 2.0</a>
<span class="sourceLineNo">006</span><a id="line.6"> * (the "License"); you may not use this file except in compliance with</a>
<span class="sourceLineNo">007</span><a id="line.7"> * the License.  You may obtain a copy of the License at</a>
<span class="sourceLineNo">008</span><a id="line.8"> *</a>
<span class="sourceLineNo">009</span><a id="line.9"> *      http://www.apache.org/licenses/LICENSE-2.0</a>
<span class="sourceLineNo">010</span><a id="line.10"> *</a>
<span class="sourceLineNo">011</span><a id="line.11"> * Unless required by applicable law or agreed to in writing, software</a>
<span class="sourceLineNo">012</span><a id="line.12"> * distributed under the License is distributed on an "AS IS" BASIS,</a>
<span class="sourceLineNo">013</span><a id="line.13"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</a>
<span class="sourceLineNo">014</span><a id="line.14"> * See the License for the specific language governing permissions and</a>
<span class="sourceLineNo">015</span><a id="line.15"> * limitations under the License.</a>
<span class="sourceLineNo">016</span><a id="line.16"> */</a>
<span class="sourceLineNo">017</span><a id="line.17">package org.apache.commons.dbutils.handlers;</a>
<span class="sourceLineNo">018</span><a id="line.18"></a>
<span class="sourceLineNo">019</span><a id="line.19">import java.sql.ResultSet;</a>
<span class="sourceLineNo">020</span><a id="line.20">import java.sql.SQLException;</a>
<span class="sourceLineNo">021</span><a id="line.21">import java.util.Map;</a>
<span class="sourceLineNo">022</span><a id="line.22"></a>
<span class="sourceLineNo">023</span><a id="line.23">import org.apache.commons.dbutils.RowProcessor;</a>
<span class="sourceLineNo">024</span><a id="line.24"></a>
<span class="sourceLineNo">025</span><a id="line.25">/**</a>
<span class="sourceLineNo">026</span><a id="line.26"> * &lt;p&gt;</a>
<span class="sourceLineNo">027</span><a id="line.27"> * {@code ResultSetHandler} implementation that returns a Map of Maps.</a>
<span class="sourceLineNo">028</span><a id="line.28"> * {@code ResultSet} rows are converted into Maps which are then stored</a>
<span class="sourceLineNo">029</span><a id="line.29"> * in a Map under the given key.</a>
<span class="sourceLineNo">030</span><a id="line.30"> * &lt;/p&gt;</a>
<span class="sourceLineNo">031</span><a id="line.31"> * &lt;p&gt;</a>
<span class="sourceLineNo">032</span><a id="line.32"> * If you had a Person table with a primary key column called ID, you could</a>
<span class="sourceLineNo">033</span><a id="line.33"> * retrieve rows from the table like this:</a>
<span class="sourceLineNo">034</span><a id="line.34"> * &lt;pre&gt;</a>
<span class="sourceLineNo">035</span><a id="line.35"> * ResultSetHandler h = new KeyedHandler("id");</a>
<span class="sourceLineNo">036</span><a id="line.36"> * Map found = (Map) queryRunner.query("select id, name, age from person", h);</a>
<span class="sourceLineNo">037</span><a id="line.37"> * Map jane = (Map) found.get(new Long(1)); // jane's id is 1</a>
<span class="sourceLineNo">038</span><a id="line.38"> * String janesName = (String) jane.get("name");</a>
<span class="sourceLineNo">039</span><a id="line.39"> * Integer janesAge = (Integer) jane.get("age");</a>
<span class="sourceLineNo">040</span><a id="line.40"> * &lt;/pre&gt;</a>
<span class="sourceLineNo">041</span><a id="line.41"> * Note that the "id" passed to KeyedHandler and "name" and "age" passed to the</a>
<span class="sourceLineNo">042</span><a id="line.42"> * returned Map's get() method can be in any case.  The data types returned for</a>
<span class="sourceLineNo">043</span><a id="line.43"> * name and age are dependent upon how your JDBC driver converts SQL column</a>
<span class="sourceLineNo">044</span><a id="line.44"> * types from the Person table into Java types.</a>
<span class="sourceLineNo">045</span><a id="line.45"> * &amp;lt;/p&amp;gt;</a>
<span class="sourceLineNo">046</span><a id="line.46"> * &lt;p&gt;This class is thread safe.&lt;/p&gt;</a>
<span class="sourceLineNo">047</span><a id="line.47"> *</a>
<span class="sourceLineNo">048</span><a id="line.48"> * @param &lt;K&gt; The type of the key</a>
<span class="sourceLineNo">049</span><a id="line.49"> * @see org.apache.commons.dbutils.ResultSetHandler</a>
<span class="sourceLineNo">050</span><a id="line.50"> * @since DbUtils 1.1</a>
<span class="sourceLineNo">051</span><a id="line.51"> */</a>
<span class="sourceLineNo">052</span><a id="line.52">public class KeyedHandler&lt;K&gt; extends AbstractKeyedHandler&lt;K, Map&lt;String, Object&gt;&gt; {</a>
<span class="sourceLineNo">053</span><a id="line.53"></a>
<span class="sourceLineNo">054</span><a id="line.54">    /**</a>
<span class="sourceLineNo">055</span><a id="line.55">     * The RowProcessor implementation to use when converting rows</a>
<span class="sourceLineNo">056</span><a id="line.56">     * into Objects.</a>
<span class="sourceLineNo">057</span><a id="line.57">     */</a>
<span class="sourceLineNo">058</span><a id="line.58">    protected final RowProcessor convert;</a>
<span class="sourceLineNo">059</span><a id="line.59"></a>
<span class="sourceLineNo">060</span><a id="line.60">    /**</a>
<span class="sourceLineNo">061</span><a id="line.61">     * The column index to retrieve key values from.  Defaults to 1.</a>
<span class="sourceLineNo">062</span><a id="line.62">     */</a>
<span class="sourceLineNo">063</span><a id="line.63">    protected final int columnIndex;</a>
<span class="sourceLineNo">064</span><a id="line.64"></a>
<span class="sourceLineNo">065</span><a id="line.65">    /**</a>
<span class="sourceLineNo">066</span><a id="line.66">     * The column name to retrieve key values from.  Either columnName or</a>
<span class="sourceLineNo">067</span><a id="line.67">     * columnIndex will be used but never both.</a>
<span class="sourceLineNo">068</span><a id="line.68">     */</a>
<span class="sourceLineNo">069</span><a id="line.69">    protected final String columnName;</a>
<span class="sourceLineNo">070</span><a id="line.70"></a>
<span class="sourceLineNo">071</span><a id="line.71">    /**</a>
<span class="sourceLineNo">072</span><a id="line.72">     * Creates a new instance of KeyedHandler.  The value of the first column</a>
<span class="sourceLineNo">073</span><a id="line.73">     * of each row will be a key in the Map.</a>
<span class="sourceLineNo">074</span><a id="line.74">     */</a>
<span class="sourceLineNo">075</span><a id="line.75">    public KeyedHandler() {</a>
<span class="sourceLineNo">076</span><a id="line.76">        this(ArrayHandler.ROW_PROCESSOR, 1, null);</a>
<span class="sourceLineNo">077</span><a id="line.77">    }</a>
<span class="sourceLineNo">078</span><a id="line.78"></a>
<span class="sourceLineNo">079</span><a id="line.79">    /**</a>
<span class="sourceLineNo">080</span><a id="line.80">     * Creates a new instance of KeyedHandler.  The value of the first column</a>
<span class="sourceLineNo">081</span><a id="line.81">     * of each row will be a key in the Map.</a>
<span class="sourceLineNo">082</span><a id="line.82">     *</a>
<span class="sourceLineNo">083</span><a id="line.83">     * @param convert The {@code RowProcessor} implementation</a>
<span class="sourceLineNo">084</span><a id="line.84">     * to use when converting rows into Maps</a>
<span class="sourceLineNo">085</span><a id="line.85">     */</a>
<span class="sourceLineNo">086</span><a id="line.86">    public KeyedHandler(final RowProcessor convert) {</a>
<span class="sourceLineNo">087</span><a id="line.87">        this(convert, 1, null);</a>
<span class="sourceLineNo">088</span><a id="line.88">    }</a>
<span class="sourceLineNo">089</span><a id="line.89"></a>
<span class="sourceLineNo">090</span><a id="line.90">    /**</a>
<span class="sourceLineNo">091</span><a id="line.91">     * Creates a new instance of KeyedHandler.</a>
<span class="sourceLineNo">092</span><a id="line.92">     *</a>
<span class="sourceLineNo">093</span><a id="line.93">     * @param columnIndex The values to use as keys in the Map are</a>
<span class="sourceLineNo">094</span><a id="line.94">     * retrieved from the column at this index.</a>
<span class="sourceLineNo">095</span><a id="line.95">     */</a>
<span class="sourceLineNo">096</span><a id="line.96">    public KeyedHandler(final int columnIndex) {</a>
<span class="sourceLineNo">097</span><a id="line.97">        this(ArrayHandler.ROW_PROCESSOR, columnIndex, null);</a>
<span class="sourceLineNo">098</span><a id="line.98">    }</a>
<span class="sourceLineNo">099</span><a id="line.99"></a>
<span class="sourceLineNo">100</span><a id="line.100">    /**</a>
<span class="sourceLineNo">101</span><a id="line.101">     * Creates a new instance of KeyedHandler.</a>
<span class="sourceLineNo">102</span><a id="line.102">     *</a>
<span class="sourceLineNo">103</span><a id="line.103">     * @param columnName The values to use as keys in the Map are</a>
<span class="sourceLineNo">104</span><a id="line.104">     * retrieved from the column with this name.</a>
<span class="sourceLineNo">105</span><a id="line.105">     */</a>
<span class="sourceLineNo">106</span><a id="line.106">    public KeyedHandler(final String columnName) {</a>
<span class="sourceLineNo">107</span><a id="line.107">        this(ArrayHandler.ROW_PROCESSOR, 1, columnName);</a>
<span class="sourceLineNo">108</span><a id="line.108">    }</a>
<span class="sourceLineNo">109</span><a id="line.109"></a>
<span class="sourceLineNo">110</span><a id="line.110">    /** Private Helper</a>
<span class="sourceLineNo">111</span><a id="line.111">     * @param convert The {@code RowProcessor} implementation</a>
<span class="sourceLineNo">112</span><a id="line.112">     * to use when converting rows into Maps</a>
<span class="sourceLineNo">113</span><a id="line.113">     * @param columnIndex The values to use as keys in the Map are</a>
<span class="sourceLineNo">114</span><a id="line.114">     * retrieved from the column at this index.</a>
<span class="sourceLineNo">115</span><a id="line.115">     * @param columnName The values to use as keys in the Map are</a>
<span class="sourceLineNo">116</span><a id="line.116">     * retrieved from the column with this name.</a>
<span class="sourceLineNo">117</span><a id="line.117">     */</a>
<span class="sourceLineNo">118</span><a id="line.118">    private KeyedHandler(final RowProcessor convert, final int columnIndex,</a>
<span class="sourceLineNo">119</span><a id="line.119">            final String columnName) {</a>
<span class="sourceLineNo">120</span><a id="line.120">        super();</a>
<span class="sourceLineNo">121</span><a id="line.121">        this.convert = convert;</a>
<span class="sourceLineNo">122</span><a id="line.122">        this.columnIndex = columnIndex;</a>
<span class="sourceLineNo">123</span><a id="line.123">        this.columnName = columnName;</a>
<span class="sourceLineNo">124</span><a id="line.124">    }</a>
<span class="sourceLineNo">125</span><a id="line.125">    /**</a>
<span class="sourceLineNo">126</span><a id="line.126">     * This factory method is called by {@code handle()} to retrieve the</a>
<span class="sourceLineNo">127</span><a id="line.127">     * key value from the current {@code ResultSet} row.  This</a>
<span class="sourceLineNo">128</span><a id="line.128">     * implementation returns {@code ResultSet.getObject()} for the</a>
<span class="sourceLineNo">129</span><a id="line.129">     * configured key column name or index.</a>
<span class="sourceLineNo">130</span><a id="line.130">     * @param rs ResultSet to create a key from</a>
<span class="sourceLineNo">131</span><a id="line.131">     * @return Object from the configured key column name/index</a>
<span class="sourceLineNo">132</span><a id="line.132">     *</a>
<span class="sourceLineNo">133</span><a id="line.133">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">134</span><a id="line.134">     * @throws ClassCastException if the class datatype does not match the column type</a>
<span class="sourceLineNo">135</span><a id="line.135">     */</a>
<span class="sourceLineNo">136</span><a id="line.136">    // We assume that the user has picked the correct type to match the column</a>
<span class="sourceLineNo">137</span><a id="line.137">    // so getObject will return the appropriate type and the cast will succeed.</a>
<span class="sourceLineNo">138</span><a id="line.138">    @SuppressWarnings("unchecked")</a>
<span class="sourceLineNo">139</span><a id="line.139">    @Override</a>
<span class="sourceLineNo">140</span><a id="line.140">    protected K createKey(final ResultSet rs) throws SQLException {</a>
<span class="sourceLineNo">141</span><a id="line.141">        return (columnName == null) ?</a>
<span class="sourceLineNo">142</span><a id="line.142">               (K) rs.getObject(columnIndex) :</a>
<span class="sourceLineNo">143</span><a id="line.143">               (K) rs.getObject(columnName);</a>
<span class="sourceLineNo">144</span><a id="line.144">    }</a>
<span class="sourceLineNo">145</span><a id="line.145"></a>
<span class="sourceLineNo">146</span><a id="line.146">    /**</a>
<span class="sourceLineNo">147</span><a id="line.147">     * This factory method is called by {@code handle()} to store the</a>
<span class="sourceLineNo">148</span><a id="line.148">     * current {@code ResultSet} row in some object. This</a>
<span class="sourceLineNo">149</span><a id="line.149">     * implementation returns a {@code Map} with case insensitive column</a>
<span class="sourceLineNo">150</span><a id="line.150">     * names as keys.  Calls to {@code map.get("COL")} and</a>
<span class="sourceLineNo">151</span><a id="line.151">     * {@code map.get("col")} return the same value.</a>
<span class="sourceLineNo">152</span><a id="line.152">     * @param rs ResultSet to create a row from</a>
<span class="sourceLineNo">153</span><a id="line.153">     * @return Object typed Map containing column names to values</a>
<span class="sourceLineNo">154</span><a id="line.154">     * @throws SQLException if a database access error occurs</a>
<span class="sourceLineNo">155</span><a id="line.155">     */</a>
<span class="sourceLineNo">156</span><a id="line.156">    @Override</a>
<span class="sourceLineNo">157</span><a id="line.157">    protected Map&lt;String, Object&gt; createRow(final ResultSet rs) throws SQLException {</a>
<span class="sourceLineNo">158</span><a id="line.158">        return this.convert.toMap(rs);</a>
<span class="sourceLineNo">159</span><a id="line.159">    }</a>
<span class="sourceLineNo">160</span><a id="line.160"></a>
<span class="sourceLineNo">161</span><a id="line.161">}</a>




























































</pre>
</div>
</main>
</body>
</html>
